#!/usr/bin/python

import sys
import math
import getopt
import types

def usage():
    print '''Usage : acgt2wyk.py [file]

    Options:
    --------
    -h, --help                   Display this text and exit.
    '''

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "h", ["help"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    if (len(args) == 0):
        usage()
        sys.exit()

    lines = init(''.join(args))

    # if file contains a sequence instead of matrix, we want to generate
    # WYK matrix corresponding to that sequence and exit
    if len(lines) == 1:
        line = lines[0]
        showMatrix(seq2wyk(line))

        return

    # else, we have file containing acgt matrix and we generate corresponding WYK matrix
    acgt_matrix = [[1 for x in xrange(len(lines[0].split()))] for y in xrange(4)]

    for i in xrange(4):
        line = (lines[i]).split()
        for j in xrange(len(line)):
            acgt_matrix[i][j] = float(line[j])

    # now we have acgt matrix

    wyk_matrix = acgt2wyk(acgt_matrix)
                
    showMatrix(wyk_matrix)

def acgt2wyk(acgt_matrix):
    wyk_matrix =  [[0 for x in xrange(len(acgt_matrix[0]))] for y in xrange(3)]

    for i in xrange(len(wyk_matrix)):
        for j in xrange(len(wyk_matrix[0])):
            if i == 0:
                wyk_matrix[i][j] = acgt_matrix[0][j]+ acgt_matrix[3][j]-acgt_matrix[1][j]- acgt_matrix[2][j]
            elif i == 1:
                wyk_matrix[i][j] = acgt_matrix[1][j]+ acgt_matrix[3][j]-acgt_matrix[0][j]- acgt_matrix[2][j]
            elif i == 2:
                wyk_matrix[i][j] = acgt_matrix[2][j]+ acgt_matrix[3][j]-acgt_matrix[1][j]- acgt_matrix[0][j]
            else:
                sys.exit("MAJOR BUG!")

    return wyk_matrix

def seq2wyk(seq):
    '''Converts given DNA sequence to WYK matrix form.'''
    base_map = {'A': 0, 'C' : 1, 'G' : 2, 'T' : 3, 'a':0, 'c':1, 'g':2, 't':3}

    acgt_matrix = [[0 for x in xrange(len(seq))] for y in xrange(4)]

    for i in xrange(len(seq)):
        acgt_matrix[base_map[seq[i]]][i] = 1

    return acgt2wyk(acgt_matrix)
    
def showMatrix(weight_mat):
    for row in weight_mat:
        for col in row:
            if (type(col) == types.IntType):
                print col,
            elif (type(col) == types.FloatType):
                print '%.2f' % col,
            else:
                print "Matrix element type not supported."
                sys.exit(2)
        print

def init(filename):
    # open file having alignments
    file = open(filename, 'r')

    lines = file.readlines()

    result = []

    for i in xrange(len(lines)):
        line = (lines[i]).strip()

        if len(line) != 0:
            result.append(line)
        else:
            pass

    # return a list cotaining all the lines in given file

    return result

if __name__ == "__main__":
    main(sys.argv[1:])
